var Moment = require("moment.js");
var DATE_LIST = [];
var DATE_YEAR = new Date().getFullYear();
var DATE_MONTH = new Date().getMonth() + 1;
var DATE_DAY = new Date().getDate();
var sFtv = [
  {
    month: 1,
    day: 1,
    name: "元旦"
  },
  {
    month: 2,
    day: 14,
    name: "情人节"
  },
  {
    month: 3,
    day: 8,
    name: "妇女节"
  },
  {
    month: 3,
    day: 12,
    name: "植树节"
  },
  {
    month: 3,
    day: 15,
    name: "消费者权益日"
  },
  {
    month: 4,
    day: 1,
    name: "愚人节"
  },
  {
    month: 5,
    day: 1,
    name: "劳动节"
  },
  {
    month: 5,
    day: 4,
    name: "青年节"
  },
  {
    month: 5,
    day: 12,
    name: "护士节"
  },
  {
    month: 6,
    day: 1,
    name: "儿童节"
  },
  {
    month: 7,
    day: 1,
    name: "建党节"
  },
  {
    month: 8,
    day: 1,
    name: "建军节"
  },
  {
    month: 9,
    day: 10,
    name: "教师节"
  },
  {
    month: 9,
    day: 28,
    name: "孔子诞辰"
  },
  {
    month: 10,
    day: 1,
    name: "国庆节"
  },
  {
    month: 10,
    day: 6,
    name: "老人节"
  },
  {
    month: 10,
    day: 24,
    name: "联合国日"
  },
  {
    month: 12,
    day: 24,
    name: "平安夜"
  },
  {
    month: 12,
    day: 25,
    name: "圣诞节"
  }
]
var createDateListData = function (maxMonth, reserved , callback) {
  var dateList = [];
  var now = new Date();
  /*
    设置日期为 年-月-01,否则可能会出现跨月的问题
    比如：2017-01-31为now ,月份直接+1（now.setMonth(now.getMonth()+1)），则会直接跳到跳到2017-03-03月份.
      原因是由于2月份没有31号，顺推下去变成了了03-03
  */
  now = new Date(now.getFullYear(), now.getMonth(), 1);
  for (var i = 0; i < maxMonth; i++) {
    var momentDate = Moment(now).add(maxMonth - (maxMonth - i), 'month').date;
    var year = momentDate.getFullYear();
    var month = momentDate.getMonth() + 1;

    var days = [];
    var totalDay = getTotalDayByMonth(year, month);
    var week = getWeek(year, month, 1);
    //-week是为了使当月第一天的日期可以正确的显示到对应的周几位置上，比如星期三(week = 2)，
    //则当月的1号是从列的第三个位置开始渲染的，前面会占用-2，-1，0的位置,从1开正常渲染
    for (var j = -week + 1; j <= totalDay; j++) {
      var tempWeek = -1;
      if (j > 0)
        tempWeek = getWeek(year, month, j);
      var clazz = '';
      if (tempWeek == 0 || tempWeek == 6)
        clazz = 'week'
      if (j < DATE_DAY && year == DATE_YEAR && month == DATE_MONTH)
        //当天之前的日期不可用
        clazz = 'unavailable ' + clazz;
      else
        clazz = '' + clazz
      for (var z = 0; z < reserved.length; z++){
        if (reserved[z].start.year / 1 <= year && reserved[z].end.year / 1 >= year) {
          if (reserved[z].start.month / 1 <= month && reserved[z].end.month / 1 >= month) {
            if (reserved[z].start.day / 1 <= j && reserved[z].end.day / 1 >= j) {
              //当天之前的日期不可用
              clazz = 'unavailable reserved ' + clazz;
            }
          }
        }
      }
      
      days.push({ day: j, class: clazz })
    }
    var dateItem = {
      id: year + '-' + month,
      year: year,
      month: month,
      days: days
    }

    dateList.push(dateItem);
  }
  //var sFtv = this.data.sFtv;
  for (let i = 0; i < dateList.length; i++) {//加入公历节日
    for (let k = 0; k < sFtv.length; k++) {
      if (dateList[i].month == sFtv[k].month) {
        let days = dateList[i].days;
        for (let j = 0; j < days.length; j++) {
          if (days[j].day == sFtv[k].day) {
            days[j].daytext = sFtv[k].name
          }
        }
      }
    }
  }
  callback(dateList);
  // this.setData({
  //   dateList: dateList
  // });
  // DATE_LIST = dateList;
}
/*
 * 获取月的总天数
 */
var getTotalDayByMonth = function (year, month) {
  month = parseInt(month, 10);
  var d = new Date(year, month, 0);
  return d.getDate();
}
/*
 * 获取月的第一天是星期几
 */
var getWeek = function (year, month, day) {
  var d = new Date(year, month - 1, day);
  return d.getDay();
}
module.exports = {
  createDateListData: createDateListData
  // function(date) {
  //   return new Moment(date);
  // }
}